package com.xzc.lease.web.admin.schedule;

import com.xzc.lease.model.entity.LeaseAgreement;
import com.xzc.lease.model.enums.LeaseStatus;
import com.xzc.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 徐生
 * 2024/10/10
 */
@Component
@Slf4j
public class ScheduleTasks {

    //@Scheduled：开启定时任务   cron：指定定时任务的具体执行时间
//    @Scheduled(cron = "* * * * * *")
//    public void test(){
//        System.out.println(new Date());
//    }

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //cron = "0 0 0 * * *" ：表示每天0时0分0秒执行一次
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        log.info("定时任务启动，检查租约是否过期。");
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        //租约结束日期 <= 当前时间，则表示该租约过期
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, new Date())
                //针对租约状态中的 已签约和退租待确认 进行检查，不需要对其他状态检查
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING)
                .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        leaseAgreementService.update(updateWrapper);
    }
}
